package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbSellerMapper;
import com.dongdongshop.pojo.TbSeller;
import com.dongdongshop.service.SellerService;
import com.dongdongsop.util.ShiroUtils;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Service
@DubboService
public class SellerServiceImpl implements SellerService {

    @Resource
    private TbSellerMapper sellerMapper;

    @Override
    public TbSeller getInfoBySellerId(String sellerId) {
        return sellerMapper.selectByPrimaryKey(sellerId);
    }

    @Override
    public int register(TbSeller seller) {
        LocalDateTime localDateTime = LocalDateTime.now();
        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        seller.setCreateTime(date);
        String salt = ShiroUtils.generateSalt(6);
        seller.setSalt(salt);
        String password = ShiroUtils.encryptPassword("MD5", seller.getPassword(), salt, 3);
        seller.setPassword(password);
        return sellerMapper.insertSelective(seller);
    }

    @Override
    public TbSeller getLogin(String sellerId) {
        return sellerMapper.selectByPrimaryKey(sellerId);
    }

    @Override
    public List<TbSeller> findSellerAll() {
        return sellerMapper.selectByExample(null);
    }

    @Override
    public TbSeller getSellerById(String sellerId) {
        return sellerMapper.selectByPrimaryKey(sellerId);
    }

    @Override
    public int updateSellerStatus(TbSeller seller) {
        return sellerMapper.updateByPrimaryKeySelective(seller);
    }
}
